package cn.aresoft.excel.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;

/**
 * @author yangfeng
 * @description:
 * @date 2024/1/31 15:39
 */
public class ExcelCellUtil {

    /**
     * 设置单元格的值
     * @param row 数据行
     * @param index 列序号
     * @param value 单元格值
     * @return Cell
     */
    public static Cell setCellValue(Row row, int index, Object value) {

        Cell cell = row.createCell(index);
        if (value instanceof Double) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Double) value);
        } else if (value instanceof Float) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Float) value);
        } else if (value instanceof String) {
            cell.setCellType(CellType.STRING);
            cell.setCellValue((String) value);
        } else if (value instanceof Integer) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Integer) value);
        } else if (value instanceof Long) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Long) value);
        } else if (value instanceof Boolean) {
            cell.setCellType(CellType.BOOLEAN);
            cell.setCellValue((Boolean) value);
        } else if (value instanceof Date) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Date) value);
        }else if (value instanceof Timestamp) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue(((Timestamp)value));
        }  else if (value instanceof Calendar) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue((Calendar) value);
        } else if (value instanceof BigDecimal) {
            cell.setCellType(CellType.NUMERIC);
            cell.setCellValue(((BigDecimal)value).doubleValue());
        }
        return cell;
    }


    /**
     * 获取单元格值
     * @param row 获取的行
     * @param column 获取单元格列号
     * @return 单元格值
     */
    public static Object getCellValue(Row row, int column) {
        Object val = null;
        Cell cell = row.getCell(column);
        if (cell != null) {
            if (cell.getCellType() == CellType.NUMERIC) {
                Double number = cell.getNumericCellValue();
                if (!DateUtil.isCellDateFormatted(cell)) {             //如果不是日期类型
                    val = number;
                } else {
                    val = DateUtil.getJavaDate(number);
                }
            } else if (cell.getCellType() == CellType.STRING) {
                if (StringUtils.isNotEmpty(cell.getStringCellValue())) {
                    val = cell.getStringCellValue();
                }
            } else if (cell.getCellType() == CellType.FORMULA) {
                val = cell.getCellFormula();
            } else if (cell.getCellType() == CellType.BOOLEAN) {
                val = cell.getBooleanCellValue();
            } else if (cell.getCellType() == CellType.ERROR) {
                val = cell.getErrorCellValue();
            }
        }
        return val;
    }

    public static boolean rowIsEmpty(Row row, int columnSize) {

        boolean isEmpty = true;
        for (int i = 0; i < columnSize; i++) {
            Object val = null;
            Cell cell = row.getCell(i);
            if (cell != null) {
                if (cell.getCellType() == CellType.NUMERIC) {
                    Double number = cell.getNumericCellValue();
                    if (!DateUtil.isCellDateFormatted(cell)) {             //如果不是日期类型
                        val = number;
                    } else {
                        val = DateUtil.getJavaDate(number);
                    }
                } else if (cell.getCellType() == CellType.STRING) {
                    if (StringUtils.isNotEmpty(cell.getStringCellValue())) {
                        val = cell.getStringCellValue();
                    } else {
                        val = null;
                    }
                } else if (cell.getCellType() == CellType.FORMULA) {
                    val = cell.getCellFormula();
                } else if (cell.getCellType() == CellType.BOOLEAN) {
                    val = cell.getBooleanCellValue();
                } else if (cell.getCellType() == CellType.ERROR) {
                    val = cell.getErrorCellValue();
                }
                if (val != null && !"".equals(val)) {
                    isEmpty = false;
                    break;
                }
            }
        }
        return isEmpty;
    }


}
